﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;


namespace Jumpton.SFA.Server.BusinessRule
{
    public class ReportManager : BusinessLogic
    {

        #region const
        private const string SELECT_ALL_ORG = "select * from View_Organization where Domain_ID={0}";
        private const string SELECT_SALES_REP = "select * from MobileUsers where OrgID={0} and DomainID={1}";
        #endregion


        public Report Load(int ID)
        {
            return (Report)ObjectRow.Load(typeof(Report), string.Format("this.ReportID='{0}'", ID));
        }

        public ObjectTable GetCourseTable()
        {
            return new ObjectTable(typeof(Report), "this.Valid='1' ");
        }

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <param name="domainID"></param>
        /// <returns></returns>
        public DataSet GetOrganization(decimal domainID)
        {
            return GetDataTable(string.Format(SELECT_ALL_ORG, domainID));
        }

        /// <summary>
        /// 获取门店负责人列表
        /// </summary>
        /// <param name="OrgID"></param>
        /// <param name="domainID"></param>
        /// <returns></returns>
        public DataTable GetSalesRep(int OrgId, decimal domainID)
        {
            return GetDataTable(string.Format(SELECT_SALES_REP, OrgId, domainID)).Tables[0];
        }

        /// <summary>
        /// 获取产品结构里面的类别 1, 包装 2, 规格 3, 口味 4, 品牌 5 其中任意一个维度
        /// </summary>
        /// <param name="PropertyID"></param>
        /// <param name="domainID"></param>
        /// <returns></returns>
        public DataTable GetProductProperty(int PropertyID, decimal domainID)
        {
            string sql = string.Empty;
            switch (PropertyID)
            {
                case 1: sql = "select [/SCL/BCAT] as Code, BEZEI as Description from TMDM_BCATT";
                    break;
                case 2: sql = "select ID as Code, Description from Package";
                    break;
                case 3: sql = "select [/SCL/SLUN] as Code, BEZEI as Description from TMDM_SLUNT";
                    break;
                case 4: sql = "select ID as Code, Description from Taste";
                    break;
                case 5: sql = "select Code, Name as Description from Brands";
                    break;
            }
            if (String.IsNullOrEmpty(sql))
                return null;
            return GetDataTable(sql).Tables[0];
        }

        /// <summary>
        /// 摘要：
        ///     执行SQL语句，返回 DataTable
        /// </summary>
        /// <param name="selectStr">存储过程的名字 or T-SQL命令</param>
        /// <returns>一个包含查询结果的 DataTable</returns>
        public DataSet GetDataTable(string selectStr)
        {
            string conStr = ConfigurationManager.ConnectionStrings["ReportingConnectionString"].ConnectionString;
            DataSet ds = new DataSet();
            //
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                cmd.CommandType = CommandType.Text; 
                SqlDataAdapter sqlda = new SqlDataAdapter(cmd);                 
                sqlda.Fill(ds);
            }
            return ds;
        }

    }
}
